package com.douqu.game.core.factory;

import com.douqu.game.core.entity.Asset;
import com.douqu.game.core.entity.InitData;
import com.douqu.game.core.entity.goods.Goods;
import com.douqu.game.core.entity.sprite.PlayerLv;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author : Bean
 * 2017-07-06 16:13
 */
public class DataFactory implements Serializable
{
    private static final long serialVersionUID = 1L;

    private static DataFactory instance = null;

    private DataFactory(){}

    public static DataFactory getInstance()
    {
        if (instance == null) {
            synchronized (DataFactory.class) {
                if (instance == null) {
                    instance = new DataFactory();
                }
            }
        }
        return instance;
    }

    /**
     * 分支服务器的时候需要用
     * @param dataFactory
     */
    public void set(DataFactory dataFactory)
    {
        instance = dataFactory;
    }


    /**
     * 初始化数据
     */
    private InitData initData;

    public void setInitData(InitData initData){
        this.initData = initData;
    }

    public InitData getInitData(){
        return this.initData;
    }

    /**
     * 最高段位
     */
    public int MAX_GRADE;


    /** 兵种配置 */
    public final static int SOLDIER_KEY = 1;
    /** 兵种动作配置 */
    public final static int SOLDIER_ANIM_KEY = 2;
    /** 等级经验配置 */
    public final static int PLAYER_LV_KEY = 3;
    /** 资源 */
    public final static int ASSET_KEY = 4;
    /** 卡牌配置 */
    public final static int CARD_KEY = 5;
    /** 卡牌升级配置 */
    public final static int CARD_LV_KEY = 6;
    /** 关卡配置 */
    public final static int LEVEL_KEY = 7;
    /** 副本配置 */
    public final static int INSTANCE_KEY = 8;
    /** 道具配置 */
    public final static int GOODS_PROP_KEY = 9;
    /** 装备配置 */
    public final static int GOODS_EQUIP_KEY = 10;
    /** 道具和装备配置 */
    public final static int GOODS_KEY = 11;
    /** 段位配置 */
    public final static int GRADE_KEY = 12;
    /** 掉落配置 */
    public final static int DROP_GROUP_KEY = 13;
    /** 技能配置 */
    public final static int SKILL_KEY = 14;
    /** 属性配置 */
    public final static int ATTRIBUTE_KEY = 15;
    /** 兵种克制关系配置 */
    public final static int SOLDIER_RESTRICT_KEY = 16;
    /** 十连抽配置 */
    public final static int LOTTERY_KEY = 17;
    /** 评星配置 */
    public final static int STAR_CHECK_KEY = 18;

    private Map<Integer,List> dataList = new HashMap<Integer,List>();

    private Map<Integer,Map> dataMap = new HashMap<Integer,Map>();

    public <T> void addData(int key, List<T> list, Map<Integer,T> map)
    {
        dataList.put(key, list);
        dataMap.put(key, map);
    }

    public <T> List<T> getDataList(int key)
    {
        return dataList.get(key);
    }

    public <T> Map<Integer, T> getDataMap(int key)
    {
        return dataMap.get(key);
    }

    public <T> T getGameObject(int key, int id)
    {
        Map map = getDataMap(key);
        if(map != null)
            return (T) map.get(id);

        return null;
    }


    public int getPlayerLv(int exp)
    {
        List<PlayerLv> list = getDataList(PLAYER_LV_KEY);
        if(list == null)
            return 0;

        int size = list.size();
        for(int i = size - 1; i >= 0; i--)
        {
            if(exp >= list.get(i).exp)
                return list.get(i).id;
        }

        return 0;
    }


//    /**
//     * 资源枚举类型对应的资源ID
//     */
//    private Map<Integer,Integer> assetType = new ConcurrentHashMap<>();
//
//    public void addAssetType(int type, int id)
//    {
//        assetType.put(type, id);
//    }
//
//    public int getAssetId(int type)
//    {
//        return assetType.get(type);
//    }


    private Map<String, String> wordMap = new ConcurrentHashMap<>();

    public void addWord(String key, String value)
    {
        wordMap.put(key, value);
    }

    public String getWord(String key, Object... params)
    {
        String result = wordMap.get(key);
        if(key.equals(WordFactory.ASSET_NOT_ENOUGH))
        {
            if(params != null && params.length > 0)
            {
                Asset asset = getGameObject(ASSET_KEY, Integer.parseInt(params[0].toString()));
                if(asset != null)
                {
                    result = MessageFormat.format(result, asset.name);
                }
            }
        }
        else if(key.equals(WordFactory.GOODS_NOT_ENOUGH))
        {
            if(params != null && params.length > 0)
            {
                Goods goods = getGameObject(GOODS_KEY, Integer.parseInt(params[0].toString()));
                if(goods != null)
                {
                    result = MessageFormat.format(result, goods.name);
                }
            }
        }

        return result;
    }


}
